package com.copyctrl.campus.portal.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.model.ApiResponse;
import com.copyctrl.campus.portal.service.AddressService;
import com.copyctrl.model.CampAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:JH
 * @version:1.0
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    AddressService service;

    @PostMapping("/{address}")
    public ApiResponse add(@PathVariable String address) {
        CampAddress campAddress = JSONUtil.toBean(address, CampAddress.class);
        int i = service.addAddress(campAddress);
        return i ==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("服务错误");
    }

    @DeleteMapping("/{id}")
    public ApiResponse delete(@PathVariable Integer id) {
        int i = service.deleteAddress(id);
        return i ==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("服务错误");
    }

    @GetMapping("/{uid}")
    public ApiResponse findByUid(@PathVariable Integer uid){
        List<CampAddress> allCampAddresses = service.findAllCampAddresses(uid);
        return ApiResponse.ofSuccess(allCampAddresses);
    }
}
